package com.alameda.listener.strategy.impl;

import com.alameda.api.user.UserService;
import com.alameda.base.constant.ProjectConstant;
import com.alameda.exception.BusinessException;
import com.alameda.listener.strategy.api.ListenerStrategy;
import com.alameda.user.pojo.UserDO;
import com.alameda.utils.PubUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.alameda.base.constant.LoginConstant.TOKEN;

/**
 * 监听策略 登录续期监听
 *
 * @author Lv
 * @date 2025/3/3
 */
@Component(ProjectConstant.StrategyConstant.RENEWAL_STRATEGY)
public class RenewalStrategyImpl implements ListenerStrategy {

    @Autowired
    private UserService userService;

    /**
     * 监听消息
     *
     * @param map 消息内容
     */
    @Override
    public void listenEvent(Map<String, Object> map) {
        String token = (String) map.get(TOKEN);
        if (PubUtils.isEmpty(token)) {
            BusinessException.throwException("未登录!");
        }

        UserDO userDO = userService.findByToken(token);
        if (PubUtils.isNull(userDO)) {
            BusinessException.throwException("未登录!");
        }
        // 处理续期
        userService.dealRenewal(userDO);
    }
}
